package com.thread.threadfirst.designMode.singleton;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 单例模式
 * 静态内部类实现
 *
 */
public class SingletonInnerClass {

    private SingletonInnerClass(){
    }

    private static class InnerClass{
        private static SingletonInnerClass singletonInnerClass=new SingletonInnerClass();
    }

    public static SingletonInnerClass getInstance(){
        return InnerClass.singletonInnerClass;
    }

    public static void main(String[] args) {
        ExecutorService executor =new ThreadPoolExecutor(3,5,200, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(3));
        for (int i=0;i<5;i++){
            executor.execute(()->{
                SingletonInnerClass instance = SingletonInnerClass.getInstance();
                System.out.println(instance);
            });
        }
        /*
        输出：
        com.thread.threadfirst.designMode.singleton.SingletonInnerClass@2382446
        com.thread.threadfirst.designMode.singleton.SingletonInnerClass@2382446
        com.thread.threadfirst.designMode.singleton.SingletonInnerClass@2382446
        com.thread.threadfirst.designMode.singleton.SingletonInnerClass@2382446
        com.thread.threadfirst.designMode.singleton.SingletonInnerClass@2382446
        */
    }
}
